import {expect} from '@jest/globals';
import fse from 'fs-extra';
import path from 'path';

import {initExecuteCommand, runHvigorAsync} from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel, IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType
} from '../../plugins/project-builder/models/constants';
import {Project} from '../../plugins/project-builder/models/project-models';
import {ICaseRecord} from '../../plugins/report-builder/report-models';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import {ProjectBuildOption, TestCaseFun} from '../../plugins/task-builder/test-case';
import {TestTaskOption} from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.ListAbility
  }
};
const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.Enable
};

const testFun: TestCaseFun = async (project: Project, taskConfig: TestTaskOption, record: ICaseRecord) => {
  const applicationPath = project.getModule('entry').moduleRootPath();
  const indexEts = path.resolve(applicationPath, 'src/main/ets/pages/Index.ets');

  // 2.entry模块依赖本模块resources中的string或者图片资源
  await fse.writeFile(indexEts, `@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Image($r('app.media.icon')).width(100).height(100);
        Text($r('app.string.EntryAbility_label')).fontSize(50).fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}`, 'utf-8');
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap',
    module: 'entry@default'
  });

  // 3.编译构建
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('选择product-default-entry-default进行make构建', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  const outputs = path.resolve(applicationPath, 'build', 'default', 'outputs');
  const outputsExists = await fse.pathExists(outputs);
  expect(outputsExists).toBe(true);
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Entry_Shared_Modules_HOS_api10_STAGE_005')
  .description('5.新模型工程entry-stage-eTS引用本模块资源')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
